/**
 * 
 */
package com.jie.eclipse.pan.editors;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.DetailsPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.MasterDetailsBlock;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;

import com.jie.eclipse.pan.Activator;
import com.jie.eclipse.pan.config.BaseConfig;
import com.jie.eclipse.pan.editors.form.PllDetailPage;
import com.jie.eclipse.pan.editors.form.PllPage;
import com.jie.eclipse.pan.utils.help.ContentProviderHelper;
import com.jie.eclipse.pan.xml.XMLNode;

/**
 * @author yanjie
 * 
 */
public class PllMasterDetailsBlock extends MasterDetailsBlock {
	private PllPage pllPage = null;
	private TreeViewer treeViewer = null;
	/**
	 * 
	 */
	public PllMasterDetailsBlock(PllPage page) {
		this.pllPage = page;
	}
	
	
	@Override
	public void createContent(IManagedForm managedForm, Composite parent) {
		super.createContent(managedForm, parent);
		sashForm.setWeights(new int[]{1,3});
	}

	@Override
	protected void createMasterPart(final IManagedForm managedForm, Composite parent) {
		sashForm.SASH_WIDTH = 1;
		sashForm.setBackground(parent.getShell().getDisplay().getSystemColor(SWT.COLOR_GRAY));
		
		FormToolkit toolkit = managedForm.getToolkit();
		// 创建一个主内容区
		Section section = toolkit.createSection(sashForm, Section.TITLE_BAR);
		section.setText("页面大纲");
		section.marginWidth = 10;
		section.marginHeight = 5;
		
		// 创建内容区的面板
		Composite right = toolkit.createComposite(section, SWT.WRAP);
		// 绘制该面板的边框，与表单的风格一致
		toolkit.paintBordersFor(right);
		GridLayout layout1 = new GridLayout();
		layout1.numColumns = 2;
		layout1.marginWidth = 2;
		layout1.marginHeight = 2;
		right.setLayout(layout1);
		
		// 创建一个树，使用toolkit对象创建
		Tree tree = toolkit.createTree(right, SWT.NULL);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = 20;
		gd.widthHint = 100;
		tree.setLayoutData(gd);
		section.setClient(right); // 书上没有这行，明明连tree都显示不了嘛。。。
		
		/*
		 * IFormPart管理了整个Part的dirty state, saving, commit, focus, selection
		 * changes等等这样的事件。
		 * 并不是表单中的每一个-空间站都需要成为一个IFormPart，最好将一组control通过实现IFormPart变成一个Part.
		 * 一般来说Section就是一个自然形成的组，所以Eclipse Form提供了一个SectionPart的实现，
		 * 它包含一个Section的对象。
		 */
		final SectionPart spart = new SectionPart(section);
		// 注册该对象到IManagedForm表单管理器中
		managedForm.addPart(spart);
		// 将普通的树包装成MVC的树
		this.treeViewer = new TreeViewer(tree);
		// 注册树的选择事件监听器
		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			// 当单击树中某一个节点时
			public void selectionChanged(SelectionChangedEvent event) {
				// 通过IManagedForm来发布IFormPart所对应的事件
				managedForm.fireSelectionChanged(spart, event.getSelection());
			}
		});
		// 设置树的内容
		treeViewer.setContentProvider(new MasterContentProvider());
		// 设置树的标签
		treeViewer.setLabelProvider(new MasterLabelProvider());
		// 设置初始化输入的类
		try{
			treeViewer.setInput(new XMLNode[]{this.pllPage.getPageNode()});
			treeViewer.expandToLevel(3);//设置树展开几级
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	protected void registerPages(DetailsPart detailsPart) {
		// 将PllDetailPage对象注册
		detailsPart.registerPage(XMLNode.class, new PllDetailPage(this.pllPage));
	}

	// 创建表单区的Action
	protected void createToolBarActions(IManagedForm managedForm) {
		final ScrolledForm form = managedForm.getForm();
		//水平布局操作
		Action hAction = new Action("horizon", Action.AS_RADIO_BUTTON) {
			public void run() {
				sashForm.setOrientation(SWT.HORIZONTAL);
				form.reflow(true);
			}
		};
		hAction.setChecked(true);
		hAction.setToolTipText("水平布局");
		hAction.setImageDescriptor(Activator
				.getImageDescriptor("icons/hor.gif"));
		
		//垂直布局操作
		Action vAction = new Action("vertical", Action.AS_RADIO_BUTTON) {
			public void run() {
				sashForm.setOrientation(SWT.VERTICAL);
				form.reflow(true);
			}
		};
		vAction.setChecked(false);
		vAction.setToolTipText("垂直布局");
		vAction.setImageDescriptor(Activator
				.getImageDescriptor("icons/ver.gif"));
		
		//将这两个操作添加到表单的工具栏管理器中
		form.getToolBarManager().add(hAction);
		form.getToolBarManager().add(vAction);
	}

	public class MasterContentProvider implements ITreeContentProvider {

		public Object[] getChildren(Object element) {
			if(this.hasChildren(element)){
				XMLNode node = ((XMLNode) element);
				if("toolbar".equalsIgnoreCase(node.getNodeName())){
					return ContentProviderHelper.getToolbarButtons(node);
				}else{
					return node.getChildNodes().toArray();
				}
			}
			else{
				return new XMLNode[0];
			}
		}
		
		public Object[] getElements(Object element) {
			 if ( element instanceof XMLNode[]) return (Object[]) element;  
			 return new Object[0]; 
		}

		public boolean hasChildren(Object element) {
			XMLNode node = ((XMLNode) element);
			if("toolbar".equalsIgnoreCase(node.getNodeName())){
				return true;
			}else if("tablecolumn".equalsIgnoreCase(node.getNodeName())){
				return false;
			}else{
				return node.getChildNodes() != null && node.getChildNodes().size() > 0 ;
			}
		}

		public Object getParent(Object element) {
			return ((XMLNode) element).getParentNode();
		}

		public void dispose() {
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}

	class MasterLabelProvider implements ILabelProvider {	
		public Image getImage(Object element) {
			XMLNode node = (XMLNode) element;
			if ((node.getChildNodes()!=null && !"tablecolumn".equalsIgnoreCase(node.getNodeName()) && node.getChildNodes().size() > 0)
					|| "toolbar".equalsIgnoreCase(node.getNodeName())
							){
				return PlatformUI.getWorkbench().getSharedImages()
						.getImage(ISharedImages.IMG_OBJ_FOLDER);
			}else{
				return PlatformUI.getWorkbench().getSharedImages()
						.getImage(ISharedImages.IMG_OBJ_FILE);
			}
		}

		public String getText(Object element) {
			if("page".equalsIgnoreCase(((XMLNode) element).getNodeName())){
				return ((XMLNode) element).gpsv("pagetitle","未知节点");
			}
			
			String text = ((XMLNode) element).gpsv(BaseConfig.TAG_LABLE,"未知节点");
			return text;
		}

		public void addListener(ILabelProviderListener listener) {
		}

		public void dispose() {

		}

		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		public void removeListener(ILabelProviderListener listener) {
		}

	}
	
	public TreeViewer getTreeViewer() {
		return treeViewer;
	}

}
